#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QMessageBox>
#include <QColorDialog>
#include <QDebug>
#include <QThread>
#include <QTimer>
#include <QSettings>
#include <QScreen>
#include <QPushButton>
#include <QDateTime>

#include "ConnectWindowRTU.h"
#include "ConnectWindowTCP.h"
#include "modbus.h"
#include "ModbusMasterProxy.h"
#include "aboutwindow.h"
#include "CustomEvent.h"


#define WINDOW_DEFAULT_WIDTH   1000
#define WINDOW_DEFAULT_HEIGHT  750


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE


#pragma pack(2)

#define ADC_MAX_CHANNEL 6

typedef struct
{
    uint16_t restart;
    uint16_t restore;

    uint32_t baudrate;
    uint16_t databits;
    uint16_t stopbits;
    uint16_t parity;
    uint16_t address;

    uint16_t  port;
    uint16_t  ip[4];
    uint16_t  gw[4];
    uint16_t  netmask[4];

    uint16_t timeout;   // 超时时间，单位：秒

}config_sys_t;

typedef struct
{
    int   zero[ADC_MAX_CHANNEL];   // 零点
    float kval[ADC_MAX_CHANNEL];   // 力值系数
}config_usr_t;

typedef struct
{
    uint32_t flag;
    config_sys_t sys;
    config_usr_t usr;
} env_t;

typedef struct
{
    float force[ADC_MAX_CHANNEL];             // 力值
    int voltage[ADC_MAX_CHANNEL];             // 电压值
    int adc[ADC_MAX_CHANNEL];                 // ADC数值
} global_var_t;

#pragma pack()

#define REG_SYS_NUM (sizeof(config_sys_t) >> 1)



class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    static void modbus_recv_callback(const void* buf, uint32_t len);
    static void modbus_send_callback(const void* buf, uint32_t len);
    void postEvent(const ModbusSendEvent& event);
    void postEvent(const ModbusRecvEvent& event);

protected:
    virtual void customEvent(QEvent* e);

private:
    Ui::MainWindow *ui;

    QLabel labelPollCount;
    QPushButton btnClear;

    ConnectWindowRTU connectWindowRTU;
    ConnectWindowTCP connectWindowTCP;
    AboutWindow aboutWindow;

    modbus_t* mbctx;
    ModbusMasterProxy* mbMasterProxy;
    QTimer timerMbPoll;
    QThread* thdMbPoll;

    void mbMasterProxyWrite(quint8 slaveAddress, quint8 functionCode, quint16 startAddress, quint32 numToWrite, QByteArray data, quint32 responseTimeout);
    void mbMasterProxyRead(quint8 slaveAddress, quint8 functionCode, quint16 startAddress, quint32 numToRead, quint32 responseTimeout);
    void mbMasterProxyReadCfg();
    void mbMasterProxyWriteCfg();
    void mbMasterProxyRestoreCfg();
    void mbMasterProxyRestartCfg();

    void enableUI(bool enable);

private slots:
    void enableDataTrack(bool enable);
    void fullScreen(bool checked);
    void connectTriggeredRTU(bool enable);
    void connectTriggeredTCP(bool enable);
    void disconnectTriggered(bool enable);

    void openPort(QString portName, int baudrate, int databits, int stopbits, int parity);
    void openPort(QString ip, quint16 port);
    void closePort(void);

    void timeout(void);

    void response(quint8 slaveAddress, quint8 functionCode, quint16 startAddress, quint32 numToReadWirte, QByteArray data);

    void pollCycle(bool checked);
    void pollSingle(bool checked);

signals:
    void mbctxMasterWrite(quint8 slaveAddress, quint8 functionCode, quint16 startAddress, quint32 numToWrite, QByteArray data, quint32 responseTimeout);
    void mbctxMasterRead(quint8 slaveAddress, quint8 functionCode, quint16 startAddress, quint32 numToRead, quint32 responseTimeout);
};

extern MainWindow* g_MainWnd;

#endif // MAINWINDOW_H
